
import sys
import os
d = os.path.dirname(__file__)
parent_path = os.path.dirname(d)
sys.path.append(parent_path)
from ulits import fstool
import datetime

##买家订单~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class Buyer(object):
    def __init__(self,buyer_id):
        self.buyer_id=buyer_id
    def find_order(self):#查询订单信息
        by_excel=fstool.Excel(parent_path+'/data/order_buyer.xls')
        by_all_data=by_excel.read_all_data('order_buyer')
        buyer_ord_lis=[]
        buyer_ord_all=[]
        for data in by_all_data:
            if self.buyer_id in data:
                buyer_ord_lis.append(data[0])
        # print(buyer_ord_lis)
        if buyer_ord_lis==[]:
            return buyer_ord_lis,buyer_ord_all
        else:
            gds_excel=fstool.Excel(parent_path+'/data/order_goods.xls')
            gds_all_data=gds_excel.read_all_data('order_goods')
            buyer_ord_all.append(gds_all_data[0])

            for i in set(buyer_ord_lis):
                for j in gds_all_data:
                    if i in j:
                        buyer_ord_all.append(j)
            return buyer_ord_lis,buyer_ord_all

    def ret_goods(self,obj):#退货   obj-订单号    
        buyer_ord_lis,buyer_ord_all=self.find_order()
        if obj in buyer_ord_lis:
            gds_excel=fstool.Excel(parent_path+'/data/order_goods.xls')
            buy_excel=fstool.Excel(parent_path+'/data/order_buyer.xls')
            sell_excel=fstool.Excel(parent_path+'/data/order_seller.xls')
            dict1={'ID':obj,'买家状态':'请求退货'}
            dict2={'ID':obj,'卖家状态':'待退货'}
            gds_excel.write_cell_data(dict1,'order_goods')
            gds_excel.write_cell_data(dict2,'order_goods')
            buy_excel.write_cell_data(dict1,'order_buyer')
            sell_excel.write_cell_data(dict2,'order_seller')
            print('退货请求已发送')
        else:
            print('您输入的订单有误')


    def take_goods(self,obj):#确认收货   编号订单
        times=''.join(str(datetime.datetime.now())[:10].split('-'))
        buyer_ord_lis,buyer_ord_all=self.find_order()
        if obj in buyer_ord_lis:
            gds_excel=fstool.Excel(parent_path+'/data/order_goods.xls')
            buy_excel=fstool.Excel(parent_path+'/data/order_buyer.xls')
            sell_excel=fstool.Excel(parent_path+'/data/order_seller.xls')
            dict1={'ID':obj,'买家状态':'已收货'}
            dict2={'ID':obj,'卖家状态':'已收货'}
            dict3={'ID':obj,'结单时间':times}
            gds_excel.write_cell_data(dict1,'order_goods')
            gds_excel.write_cell_data(dict2,'order_goods')
            gds_excel.write_cell_data(dict3,'order_goods')
            buy_excel.write_cell_data(dict1,'order_buyer')
            sell_excel.write_cell_data(dict2,'order_seller')
            print('已确认收货')

def ord_func(buyer_id):
    while True:
        print("""---------------------
        1.查询订单
        2.退货
        3.收货
        4.返回
        5.退出
----------------------""")
        buyer=Buyer(buyer_id)
        num=input('输入对应编号：').strip()
        if num=='1':
            buyer_ord_lis,buyer_ord_all=buyer.find_order()
            if buyer_ord_lis==[]:
                print('您还未购买商品^~^')
            else:
                print('您购买的商品有：')
                for i in buyer_ord_all[1:]:
                    print('订单ID：%s   商品名称：%s   商品数量：%s   商品单价%s   买家状态：%s   卖家状态：%s   下单时间：%s   结单时间：%s'%(i[0],i[1],i[2],i[3],i[4],i[5],i[6],i[7]))
        elif num =='2':
            for j,k in enumerate(buyer.find_order()[1],0):
                print(j,k[:2])
            ret_num=input('输入要退的订单编号：').strip()
            if ret_num.isdigit():
                ret_num=int(ret_num)
                if 0<ret_num<len(buyer.find_order()[1]):
                    if buyer.find_order()[1][ret_num][5]!=('待退货' and '已退货'):
                        ret_id=buyer.find_order()[1][ret_num][0]
                        buyer.ret_goods(ret_id)
                    else:
                        print('退货，请勿重复操作！')
                else:
                    print('编号不正确！')
            else:
                print('输入错误！')
        elif num=='3':
            for j,k in enumerate(buyer.find_order()[1],0):
                print(j,k[:2])
            ret_num=input('输入要确认收货的订单编号：').strip()
            if ret_num.isdigit():
                ret_num=int(ret_num)
                if 0<ret_num<len(buyer.find_order()[1]):
                    if buyer.find_order()[1][ret_num][4]=='待收货':
                        ret_id=buyer.find_order()[1][ret_num][0]
                        buyer.take_goods(ret_id)
                    else:
                        print('您没有权限！')
                else:
                    print('编号不正确！')
            else:
                print('输入错误！')
        elif num=='4':
            print('已返回')
            return 
        elif num=='5':
            exit('已退出')
        else:
            print('输入错误！')

if __name__=='__main__':

    ord_func('12345678901')
